home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / fp_adpcm / x2adpcm / source / x2adpcm.c < prev   
C/C++ Source or Header  |  1995-08-25  |  5KB  |  214 lines

  1.  
  2. /* Convert any known sample format (datatypes!) to ADPCM */
  3. /* Written in 1995 by Christian Buchner. This is Public Domain */
  4.  
  5.  
  6. /* Includes */
  7.  
  8. #include <proto/dos.h>
  9. #include <proto/exec.h>
  10. #include <proto/intuition.h>
  11. #include <proto/datatypes.h>
  12. #include <clib/alib_stdio_protos.h>
  13. #include <libraries/dos.h>
  14. #include <intuition/intuition.h>
  15. #include <exec/io.h>
  16. #include <exec/memory.h>
  17. #include <exec/execbase.h>
  18. #include <datatypes/datatypes.h>
  19. #include <datatypes/datatypesclass.h>
  20. #include <datatypes/soundclass.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23.  
  24. /*******************************************************************************/
  25.  
  26. extern __asm ULONG CompressADPCM2(        register __a0 UBYTE *Source,
  27.                                         register __d0 ULONG Length,
  28.                                         register __a1 UBYTE *Destination,
  29.                                         register __d1 ULONG JoinCode    );
  30.  
  31. extern __asm ULONG CompressADPCM3(        register __a0 UBYTE *Source,
  32.                                         register __d0 ULONG Length,
  33.                                         register __a1 UBYTE *Destination,
  34.                                         register __d1 ULONG JoinCode    );
  35.  
  36. /*******************************************************************************/
  37.  
  38.  
  39. #define INPUT_SIZE 16384
  40.  
  41.  
  42. /* Library bases */
  43.  
  44. struct DosLibrary *DOSBase;
  45. struct Library *DataTypesBase;
  46. struct IntuitionBase *IntuitionBase;
  47.  
  48.  
  49. UBYTE *Template="FROM/A,TO/K/A,BITS/K/N";
  50.  
  51. struct ArgArray
  52. {
  53.     UBYTE *aa_From;
  54.     UBYTE *aa_To;
  55.     ULONG *aa_Bits;
  56. };
  57.  
  58. struct ArgArray AA;
  59. struct RDArgs *RDArgs;
  60. UBYTE ProgName[60];
  61.  
  62. /*******************************************************************************/
  63.  
  64.  
  65. /* void __saveds main() */
  66.  
  67. LONG __saveds main()
  68. {
  69.     ULONG Bits=2;
  70.     
  71.     if (DOSBase=(struct DosLibrary*)OpenLibrary("dos.library",37))
  72.     {
  73.         if (DataTypesBase=OpenLibrary("datatypes.library",39))
  74.         {
  75.             if (!GetProgramName(ProgName,sizeof(ProgName))) strcpy(ProgName,"CDRipper");
  76.             
  77.             if (!(RDArgs=ReadArgs(Template,(LONG *)&AA,0)))
  78.             {
  79.                 PrintFault(IoErr(),ProgName);
  80.             }
  81.             else
  82.             {
  83.                 if (AA.aa_Bits) Bits= *AA.aa_Bits;
  84.                 
  85.                 if (Bits != 2 && Bits != 3)
  86.                 {
  87.                     Printf("Illegal bit number. Use 2 for ADPCM2 and 3 for ADPCM3.\n");
  88.                 }
  89.                 else
  90.                 {
  91.                     Object *Sound;
  92.                     
  93.                     if (Sound=NewDTObject(AA.aa_From, DTA_GroupID, GID_SOUND, TAG_DONE))
  94.                     {
  95.                         UBYTE *Sample;
  96.                         ULONG Length;
  97.                         ULONG Period;
  98.                         
  99.                         if (GetDTAttrs(Sound, SDTA_Sample, &Sample, SDTA_SampleLength, &Length, SDTA_Period, &Period, TAG_DONE) != 3)
  100.                         {
  101.                             Printf("Cannot get sample attributes!\n");
  102.                         }
  103.                         else
  104.                         {
  105.                             UBYTE *ADPCMBuffer;
  106.                             ULONG ADPCMLen;
  107.                             
  108.                             if (Bits==2) ADPCMLen=(INPUT_SIZE+3)/4;
  109.                             if (Bits==3) ADPCMLen=(INPUT_SIZE+7)/8*3;
  110.                             
  111.                             if (!(ADPCMBuffer=AllocVec(ADPCMLen,MEMF_ANY|MEMF_CLEAR)))
  112.                             {
  113.                                 Printf("No memory for ADPCM buffer!\n");
  114.                             }
  115.                             else
  116.                             {
  117.                                 BPTR OutFile;
  118.                                 
  119.                                 if (!(OutFile=Open(AA.aa_To,MODE_NEWFILE)))
  120.                                 {
  121.                                     Printf("Cannot open '%s' for output: ",AA.aa_To);
  122.                                     PrintFault(IoErr(),NULL);
  123.                                 }
  124.                                 else
  125.                                 {
  126.                                     ULONG Frequency=(*(struct ExecBase**)(4))->ex_EClockFrequency*5/Period;
  127.                                     ULONG Offset;
  128.                                     ULONG JoinCode=0;
  129.                                     
  130.                                     if (Bits==2) Write(OutFile,"ADPCM2", 6);
  131.                                     if (Bits==3) Write(OutFile,"ADPCM3", 6);
  132.                                     
  133.                                     Write(OutFile,&Frequency, sizeof(Frequency));
  134.                                     
  135.                                     for (Offset=0; Offset<Length; )
  136.                                     {
  137.                                         ULONG Left=Length-Offset;
  138.                                         ULONG Do = Left < INPUT_SIZE ?
  139.                                                    Left : INPUT_SIZE;
  140.                                         
  141.                                         Printf("\rCompressing/Saving (%02ld%%)",100*Offset/Length);
  142.                                         
  143.                                         if (Bits==2) ADPCMLen=(Do+3)/4;
  144.                                         if (Bits==3) ADPCMLen=(Do+7)/8*3;
  145.                                         
  146.                                         if (Bits==2) JoinCode=CompressADPCM2(Sample+Offset, Do, ADPCMBuffer, JoinCode);
  147.                                         if (Bits==3) JoinCode=CompressADPCM3(Sample+Offset, Do, ADPCMBuffer, JoinCode);
  148.                                         
  149.                                         Write(OutFile, ADPCMBuffer, ADPCMLen);
  150.                                         
  151.                                         Offset+=Do;
  152.                                         
  153.                                         if (CheckSignal(SIGBREAKF_CTRL_C))
  154.                                         {
  155.                                             PrintFault(ERROR_BREAK,NULL);
  156.                                             break;
  157.                                         }
  158.                                     }
  159.                                     Printf("\rCompressing/Saving finished\n",100*(Length-Offset)/Length);
  160.                                     
  161.                                     Close(OutFile);
  162.                                 }
  163.                                 FreeVec(ADPCMBuffer);
  164.                             }
  165.                         }
  166.                         DisposeDTObject(Sound);
  167.                     }
  168.                     else
  169.                     {
  170.                         LONG Error=IoErr();
  171.                         
  172.                         if (Error>=2000)
  173.                         {
  174.                             Printf("%s: %s\n",AA.aa_From,GetDTString(Error));
  175.                         }
  176.                         else
  177.                         {
  178.                             PrintFault(Error, AA.aa_From);
  179.                         }
  180.                     }
  181.                 }
  182.                 FreeArgs(RDArgs);
  183.             }
  184.             CloseLibrary(DataTypesBase);
  185.         }
  186.         CloseLibrary(DOSBase);
  187.     }
  188. }
  189.  
  190.  
  191. /*******************************************************************************/
  192.  
  193. /* Show a message to the user */
  194.  
  195. void __stdargs Message(UBYTE *Msg,...)
  196. {
  197.     va_list Arg;
  198.     struct EasyStruct Req={sizeof(struct EasyStruct),0,"X2ADPCM message",0,"Okay"};
  199.     Req.es_TextFormat=Msg;
  200.     va_start(Arg,Msg);
  201.     
  202.     if (IntuitionBase)
  203.     {
  204.         EasyRequestArgs(NULL,&Req,0,Arg);
  205.     }
  206.     else
  207.     {
  208.         VPrintf(Msg,Arg);
  209.         Printf("\n");
  210.     }
  211.     
  212.     va_end(Arg);
  213. }
  214.